package angry.courses.beans;

import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.ejb.EJB;

import java.util.List;
import java.util.ArrayList;

import angry.courses.entities.CourseEntity;

@Stateless(mappedName="angry.courses.beans.CourseBean")
@Remote
public class CourseBean implements Course {

	@EJB
	private CourseDAO courseDAO;

	public CourseEntity getCourse(String name) {
		return courseDAO.retrieve(name);
	}
	
	public List<String> getCourseNamesForSemester(int sem) {
		List<CourseEntity> courses = courseDAO.getAllForSemester(sem);
		List<String> courseNames = new ArrayList<String>();
		for (CourseEntity course : courses) {
			courseNames.add(course.getName());
		}
		return courseNames;
	}

	public List<String> getCourseNames() {
		List<String> courseNames = new ArrayList<String>();
		courseNames.addAll(getCourseNamesForSemester(1));
		courseNames.addAll(getCourseNamesForSemester(2));
		return courseNames;
	}
	
	public CourseEntity createCourse(String name, int year, int sem, String prof) {
		CourseEntity newCourse = new CourseEntity(name, year, sem, prof);
		courseDAO.create(newCourse);
		return newCourse;
	}
	
	public void removeCourse(CourseEntity course) {
		courseDAO.remove(course);
	}

	public List<CourseEntity> getCourses(int year, int semester) {
		return courseDAO.getForYearAndSemester(year, semester);
	}
}
